﻿using System;
using System.Runtime.Serialization;
using System.Linq.Expressions;

namespace ExpressionSerialization
{
  [KnownType(typeof(XExpressionList))]
  [DataContract]
  public class XInvocationExpression : XExpression
  {
    [DataMember]
    public XExpression Expression  {get; set; }

    [DataMember]
    public XExpressionList Arguments  {get; set; }

    public override void FromExpression(Expression source_)
    {
      base.FromExpression(source_);
      InvocationExpression exp = source_ as InvocationExpression;
      Expression = XExpression.CreateFromExpression(exp.Expression);
      Arguments = new XExpressionList(exp.Arguments);
    }

    public override Expression ToExpression(Type dataContract_, System.Collections.Generic.List<ParameterExpression> parameters_)
    {
      return System.Linq.Expressions.Expression.Invoke(Expression.ToExpression(dataContract_, parameters_), Arguments.ToExpressions(dataContract_, parameters_));
    }
  }
}
